#!/usr/bin/env python2.5

import struct, time, os, re,decimal
from serial import Serial

CPUStatisticsParser = re.compile(r'.+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)')

def getLoadStatistics():
    result = {}
    for line in os.popen('/usr/sbin/iostat'):
        match = CPUStatisticsParser.match(line)
        if match:
            return [float(match.group(4)), float(match.group(5)), float(match.group(6))]


serial = Serial('/dev/tty.usbserial-A60049EO', 19200)
while True:
    stats = getLoadStatistics()
    if stats:
        load = stats[0]
        if load > 1.0:
            load = 1.0
        load = int(load * 16)
        print "%s = %s" % (stats[0], load)
        serial.write(struct.pack("B", load))
        time.sleep(1)
